package com.eati.cm.auth;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import com.eati.cm.utils.CastUtil;
import com.eati.cm.view.Result;
import com.eati.cm.view.ResultStatus;

@Component
public class AuthFailHandler extends SimpleUrlAuthenticationFailureHandler{

	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException exception) throws IOException, ServletException {
		Result<Object> res = new Result<>();
       res.status = ResultStatus.BAD_REQ;
       res.finishedDate = new Date();
       response.setStatus(403);
       response.setContentType("application/json;charset=UTF-8");
       response.getWriter().append(
    		   CastUtil.toJson(res)
    		   );
	}
}
